package t20211204;

/**
 * @Author: DengJia
 * @Date: 2021/12/4 20:12
 * @Description: 一个简单的 “迭代器” 设计模式
 */

public class Sequence {
    private Object[] items;
    private int index = 0;

    public Sequence(int size) {
        items = new Object[size];
    }

    public void add(Object o) {
        if (index < items.length) {
            items[index++] = o;
        }
    }

    private class SequenceSelector implements Selector {
        private int i = 0;

        @Override
        public boolean end() {
            return i == items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }
    }

    public Selector selector() {
        return new SequenceSelector();
    }

    public static void main(String[] args) {
        Sequence sequence = new Sequence(10);
        for (int i = 0; i < 10; i++) {
            sequence.add(Integer.toString(i));
        }
        Selector selector = sequence.selector();
//        while (!selector.end()) {
//            System.out.println(selector.current());
//            selector.next();
//        }

        Sequence ss = new Sequence(10);
        for (int i = 0; i < 10; i++) {
            ss.add(new StringClass("STR : " + i));
        }
        System.out.println();
    }
}

class StringClass {
    private String val;

    public StringClass(String val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return this.val;
    }
}
